import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**庭院管理器属性枚举*/
export const enum E_GardenManager {
	/**上次日常重置时间*/
	LAST_DAILY_RESET_TIME = 0,
	/**当前植物成长值*/
	CURRENT_GROWTH = 1,
	/**最近浇水时间*/
	LAST_WATER_TIME = 2,
	/**当前植物ID*/
	CURRENT_PLANT_ID = 3,
	/**植物状态 (0=空地, 1=种植中, 2=成熟可收获,3=成熟已收获)*/
	PLANT_STATUS = 4,
	/**枚举数量*/
	EnumCount = 5
}


/**庭院管理器*/
export class GardenManager {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_GARDEN_MANAGER_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_GARDEN_MANAGER_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: GardenManager[] = [];

	/**创建入口*/
	public static create(json?: string): GardenManager {
		const c = this.POOL.pop() || new GardenManager();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**上次日常重置时间 (uint32)*/
	protected _lastDailyResetTime: number;
	/**当前植物成长值 (uint32)*/
	protected _currentGrowth: number;
	/**最近浇水时间 (uint32)*/
	protected _lastWaterTime: number;
	/**当前植物ID (uint32)*/
	protected _currentPlantId: number;
	/**植物状态 (0=空地, 1=种植中, 2=成熟可收获,3=成熟已收获) (uint8)*/
	protected _plantStatus: number;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (GardenManager.POOL.indexOf(this) === -1) {
			GardenManager.POOL.push(this);
		}
	}

	/**上次日常重置时间*/
	public get lastDailyResetTime() {
		return this._lastDailyResetTime;
	}


	/**上次日常重置时间*/
	public set lastDailyResetTime(v: number) {
		this._lastDailyResetTime = v;
	}


	/**当前植物成长值*/
	public get currentGrowth() {
		return this._currentGrowth;
	}


	/**当前植物成长值*/
	public set currentGrowth(v: number) {
		this._currentGrowth = v;
	}


	/**最近浇水时间*/
	public get lastWaterTime() {
		return this._lastWaterTime;
	}


	/**最近浇水时间*/
	public set lastWaterTime(v: number) {
		this._lastWaterTime = v;
	}


	/**当前植物ID*/
	public get currentPlantId() {
		return this._currentPlantId;
	}


	/**当前植物ID*/
	public set currentPlantId(v: number) {
		this._currentPlantId = v;
	}


	/**植物状态 (0=空地, 1=种植中, 2=成熟可收获,3=成熟已收获)*/
	public get plantStatus() {
		return this._plantStatus;
	}


	/**植物状态 (0=空地, 1=种植中, 2=成熟可收获,3=成熟已收获)*/
	public set plantStatus(v: number) {
		this._plantStatus = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_GardenManager): any {
		switch (k) {
			case E_GardenManager.LAST_DAILY_RESET_TIME: return this._lastDailyResetTime;
			case E_GardenManager.CURRENT_GROWTH: return this._currentGrowth;
			case E_GardenManager.LAST_WATER_TIME: return this._lastWaterTime;
			case E_GardenManager.CURRENT_PLANT_ID: return this._currentPlantId;
			case E_GardenManager.PLANT_STATUS: return this._plantStatus;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_GardenManager, v: any): void {
		switch (k) {
			case E_GardenManager.LAST_DAILY_RESET_TIME: this._lastDailyResetTime = v; break;
			case E_GardenManager.CURRENT_GROWTH: this._currentGrowth = v; break;
			case E_GardenManager.LAST_WATER_TIME: this._lastWaterTime = v; break;
			case E_GardenManager.CURRENT_PLANT_ID: this._currentPlantId = v; break;
			case E_GardenManager.PLANT_STATUS: this._plantStatus = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//上次日常重置时间
		this._lastDailyResetTime = 0;
		//当前植物成长值
		this._currentGrowth = 0;
		//最近浇水时间
		this._lastWaterTime = 0;
		//当前植物ID
		this._currentPlantId = 0;
		//植物状态 (0=空地, 1=种植中, 2=成熟可收获,3=成熟已收获)
		this._plantStatus = 0;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._lastDailyResetTime) {
			json.lastDailyResetTime = this._lastDailyResetTime;
		}
		if (this._currentGrowth) {
			json.currentGrowth = this._currentGrowth;
		}
		if (this._lastWaterTime) {
			json.lastWaterTime = this._lastWaterTime;
		}
		if (this._currentPlantId) {
			json.currentPlantId = this._currentPlantId;
		}
		if (this._plantStatus) {
			json.plantStatus = this._plantStatus;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.lastDailyResetTime) {
			this._lastDailyResetTime = json.lastDailyResetTime;
		}
		if (json.currentGrowth) {
			this._currentGrowth = json.currentGrowth;
		}
		if (json.lastWaterTime) {
			this._lastWaterTime = json.lastWaterTime;
		}
		if (json.currentPlantId) {
			this._currentPlantId = json.currentPlantId;
		}
		if (json.plantStatus) {
			this._plantStatus = json.plantStatus;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeUint32(this._lastDailyResetTime);
		byte.writeUint32(this._currentGrowth);
		byte.writeUint32(this._lastWaterTime);
		byte.writeUint32(this._currentPlantId);
		byte.writeUint8(this._plantStatus);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._lastDailyResetTime = byte.readUint32();
		this._currentGrowth = byte.readUint32();
		this._lastWaterTime = byte.readUint32();
		this._currentPlantId = byte.readUint32();
		this._plantStatus = byte.readUint8();
		return this;
	}


	/**克隆*/
	public clone(): GardenManager {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = GardenManager.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = GardenManager.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//上次日常重置时间
			if (idx === E_GardenManager.LAST_DAILY_RESET_TIME) {
				const oldv = this._lastDailyResetTime;
				this._lastDailyResetTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._lastDailyResetTime, oldv);
				i += 2;
				continue;
			}
			//当前植物成长值
			if (idx === E_GardenManager.CURRENT_GROWTH) {
				const oldv = this._currentGrowth;
				this._currentGrowth = us[i + 1];
				IAsync.dispatch(eventID, idx, this._currentGrowth, oldv);
				i += 2;
				continue;
			}
			//最近浇水时间
			if (idx === E_GardenManager.LAST_WATER_TIME) {
				const oldv = this._lastWaterTime;
				this._lastWaterTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._lastWaterTime, oldv);
				i += 2;
				continue;
			}
			//当前植物ID
			if (idx === E_GardenManager.CURRENT_PLANT_ID) {
				const oldv = this._currentPlantId;
				this._currentPlantId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._currentPlantId, oldv);
				i += 2;
				continue;
			}
			//植物状态 (0=空地, 1=种植中, 2=成熟可收获,3=成熟已收获)
			if (idx === E_GardenManager.PLANT_STATUS) {
				const oldv = this._plantStatus;
				this._plantStatus = us[i + 1];
				IAsync.dispatch(eventID, idx, this._plantStatus, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in GardenManager with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: GardenManager): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._lastDailyResetTime !== other._lastDailyResetTime) {
			updates.push(E_GardenManager.LAST_DAILY_RESET_TIME, this._lastDailyResetTime);
		}
		if (this._currentGrowth !== other._currentGrowth) {
			updates.push(E_GardenManager.CURRENT_GROWTH, this._currentGrowth);
		}
		if (this._lastWaterTime !== other._lastWaterTime) {
			updates.push(E_GardenManager.LAST_WATER_TIME, this._lastWaterTime);
		}
		if (this._currentPlantId !== other._currentPlantId) {
			updates.push(E_GardenManager.CURRENT_PLANT_ID, this._currentPlantId);
		}
		if (this._plantStatus !== other._plantStatus) {
			updates.push(E_GardenManager.PLANT_STATUS, this._plantStatus);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(GardenManager.TOTAL_EVENT_ID, this.getV(0));
	}

}
